import { boot } from "quasar/wrappers"
import { LoadingBar, Loading, QSpinnerGears } from "quasar"
// import { useUserStore } from 'src/stores/user'
const whiteList = ["Login", "Init"]
import { useUserStore } from "@/stores/use_user_store"
// import { useRouterStore } from '@/stores/use_router_store'

LoadingBar.setDefaults({
    color: "red",
    size: "4px",
    position: "top",
})

function startLoading() {
    Loading.show({
        spinner: QSpinnerGears,
        message: "系统加载中……",
    })
    LoadingBar.start()
}

function stopLoading() {
    Loading.hide()
    LoadingBar.stop()
}

export default boot(({ router }) => {
    router.beforeEach(async (to, from, next) => {
        const userStore = useUserStore()
        startLoading()
        const token = userStore.token

        // 在白名单中的判断情况
        if (whiteList.indexOf(to.name) > -1) {
            next()
            stopLoading()
        } else {
            // 不在白名单中并且已经登录的时候
            // console.log(`不在白名单中并且已经登录的时候 ing👇`)
            if (token) {
                next()
                stopLoading()
            }
            // 不在白名单中并且未登录的时候
            if (!token) {
                next({
                    name: "Login",
                    query: {
                        redirect: document.location.hash,
                    },
                })
                stopLoading()
            }
        }
    })

    router.afterEach(() => {
        console.log(`stopLoading ing👇`)
        stopLoading()
    })

    router.onError(() => {
        stopLoading()
    })
})
